#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <unistd.h>
#include <cmath>
#include <cstdlib>
#include "CInitGL.h"
#define ESCAPE 27


int window;
 float rtri = 0.0f;
//extern bool* keyStates  = new bool[256];
 float depth = -20.0f,Movex = 0.0f, Movey = 0.0f;
 float xpos = 0, ypos = 0, zpos = 0, xrot = 0, yrot = 0, angle=0.0;
 float positionz[10];
 float positionx[10];

CInitGL *CInitGL::instance = 0;

void CInitGL::setInstance(CInitGL *framework) {
	instance = framework;
}


void CInitGL::DisplayWrapper(float dTime)
{
    instance->display(dTime);
}


void CInitGL::InitWrap(int Width,int Height)
{
    instance->Init(Width,Height);
}

void CInitGL::FancyRotateWrap(void)
{
    instance->FancyRotate();
}

void CInitGL::DrawGLSceneWrap(void)
{
    instance->DrawGLScene();
}

void CInitGL::ReSizeGLSceneWrap(int Width, int Height)
{
    instance->ReSizeGLScene(Width,Height);
}

void CInitGL::KeyPressedWrap(unsigned char key, int x, int y)
{
    instance->keyboard(key,x,y);
}

void CInitGL::SpecialKeyWrap(int key, int x, int y)
{
    instance->specialKeyboard(key,x,y);
}

void CInitGL::Init(int Width,int Height)
{
    glViewport(0,0,800,600);
     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  glClearDepth(1.0);
  glDepthFunc(GL_LESS);
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_LIGHTING);
  glEnable(GL_LIGHT0);
  glShadeModel(GL_SMOOTH);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f);
  glMatrixMode(GL_MODELVIEW);
}
void CInitGL::FancyRotate(void)
{

   glRotatef(xrot,1.0,0.0,0.0);
    glRotatef(yrot,0.0,1.0,0.0);
    glTranslated(-xpos,-ypos,-zpos);
}



void CInitGL::DrawGLScene(void)

    {
  glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  glLoadIdentity();

  glTranslatef(Movex,Movey,depth);		// Move Left 1.5 Units And Into The Screen 6.0

FancyRotate();

angle++;

  glRotatef(rtri,0.0f,1.0f,0.0f);		// Rotate The Pyramid On The Y axis
   //glutWireCube(2.0f);
  glBegin(GL_POLYGON);

  // front
  glColor3f(1.0f,0.0f,0.0f);
  glVertex3f(0.0f, 1.0f, 0.0f);
  glColor3f(0.0f,1.0f,0.0f);
  glVertex3f(-1.0f,-1.0f, 1.0f);
  glColor3f(0.0f,0.0f,1.0f);
  glVertex3f(1.0f,-1.0f, 1.0f);

  // right
  glColor3f(1.0f,0.0f,0.0f);
  glVertex3f( 0.0f, 1.0f, 0.0f);
  glColor3f(0.0f,0.0f,1.0f);
  glVertex3f( 1.0f,-1.0f, 1.0f);
  glColor3f(0.0f,1.0f,0.0f);
  glVertex3f( 1.0f,-1.0f, -1.0f);

  // back
  glColor3f(1.0f,0.0f,0.0f);
  glVertex3f( 0.0f, 1.0f, 0.0f);
  glColor3f(0.0f,1.0f,0.0f);
  glVertex3f( 1.0f,-1.0f, -1.0f);
  glColor3f(0.0f,0.0f,1.0f);
  glVertex3f(-1.0f,-1.0f, -1.0f);

  // left
  glColor3f(1.0f,0.0f,0.0f);
  glVertex3f( 0.0f, 1.0f, 0.0f);
  glColor3f(0.0f,0.0f,1.0f);
  glVertex3f(-1.0f,-1.0f,-1.0f);
  glColor3f(0.0f,1.0f,0.0f);
  glVertex3f(-1.0f,-1.0f, 1.0f);

  glEnd();

  rtri+=0.07f;
  glutSwapBuffers();
}


void CInitGL::keyboard(unsigned char key, int x, int y)
{
    if (key == ESCAPE)
    {
      glutDestroyWindow(window);
      exit(0);
    }
     if (key=='q')
    {
    xrot += 1;
    if (xrot >360) xrot -= 360;
    }

    if (key=='e')
    {
    xrot -= 1;
    if (xrot < -360) xrot += 360;
    }

    if (key=='w')
    {
    float xrotrad, yrotrad;
    yrotrad = (yrot / 180 * 3.141592654f);
    xrotrad = (xrot / 180 * 3.141592654f);
    xpos += float(sin(yrotrad)) ;
    zpos -= float(cos(yrotrad)) ;
    ypos -= float(sin(xrotrad)) ;
    }

    if (key=='s')///////////////////////////////mess with this later
    {
    float xrotrad, yrotrad;
    yrotrad = (yrot / 180 * 3.141592654f);
    xrotrad = (xrot / 180 * 3.141592654f);
    xpos -= float(sin(yrotrad));
    zpos += float(cos(yrotrad)) ;
    ypos += float(sin(xrotrad));
    }

    if (key=='d')
    {
    yrot += 1;
    if (yrot >360) yrot -= 360;
    }

    if (key=='a')
    {
    yrot -= 1;
    if (yrot < -360)yrot += 360;
    }
}


void CInitGL::specialKeyboard(int key, int x, int y)
{

   if (key == GLUT_KEY_UP)
        {
            Movey += 0.5f;
            glutPostRedisplay();
        }
    if (key == GLUT_KEY_DOWN)
        {
            Movey += -0.5f;
            glutPostRedisplay();
        }
    if (key == GLUT_KEY_LEFT)
        {
            Movex += -0.5f;
            glutPostRedisplay();
        }
    if (key == GLUT_KEY_RIGHT)
        {
            Movex += 0.5f;
            glutPostRedisplay();
        }
    if (key == GLUT_KEY_PAGE_UP)
        {
            depth += 0.5f;
            glutPostRedisplay();
        }
    if (key == GLUT_KEY_PAGE_DOWN)
        {
            depth += -0.5f;
            glutPostRedisplay();
        }
}


void CInitGL::ReSizeGLScene(int Width, int Height)
{
  if (Height==0)
    Height=1;

  glViewport(0, 0, Width, Height);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

  gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f);
  glMatrixMode(GL_MODELVIEW);
}



void CInitGL::StartInitGL(int argc, char *argv[]) {
glEnable(GL_DEPTH_TEST);
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);
	glutInitWindowSize(640, 480);
		glutInitWindowPosition(0, 0);
	window = glutCreateWindow("Sector7");
    glutDisplayFunc(DrawGLSceneWrap);
    glutFullScreen();
    glutIdleFunc(DrawGLSceneWrap);
    glutReshapeFunc(ReSizeGLSceneWrap);
	glutKeyboardFunc(KeyPressedWrap);
	//glutKeyboardUpFunc(keyboardUpWrapper);
	glutSpecialFunc(SpecialKeyWrap);
	//glutSpecialUpFunc(specialKeyboardUpWrapper);

	Init(640,480);
	glutMainLoop();
}
